package com.hxh.crud.controller;

import com.github.pagehelper.PageInfo;
import com.hxh.crud.bean.Department;
import com.hxh.crud.service.DepartmentService;
import com.hxh.crud.util.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreatedBy: hxh
 * @Date: 2020/2/28 14:10
 * @Description: 部门控制器
 * @TODO: 加油吧！骚年！
 */
@CrossOrigin
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/depts")
    public CommonResponse getEmps(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Department> depts = departmentService.getDepts(pageNum, pageSize);
        return CommonResponse.success("查询成功！", depts);
    }

    // @RequestMapping支持映射多个地址
    @RequestMapping({"/checkDept/{deptName}", "/checkDept/{deptName}/{deptId}"})
    public CommonResponse checkDept(@PathVariable(required = false) String deptId,
                                    @PathVariable String deptName) {
        boolean flag = departmentService.checkDept(deptName, deptId);
        if (flag) {
            return CommonResponse.failure("部门名重复，请更换部门名");
        }
        return CommonResponse.success("部门名未重复");
    }

    @PostMapping("/dept/{deptName}")
    public CommonResponse addDept(@PathVariable String deptName) {
        departmentService.addDept(deptName);
        return CommonResponse.success("添加成功！");
    }

    @PutMapping("/dept/{deptName}/{deptId}")
    public CommonResponse editDept(@PathVariable String deptName,
                                   @PathVariable String deptId) {
        departmentService.editDept(deptName, deptId);
        return CommonResponse.success("修改成功！");
    }

    @DeleteMapping("/dept")
    public CommonResponse delDept(@RequestBody List<Department> departmentList) {
        departmentService.delDept(departmentList);
        return CommonResponse.success("删除成功！");
    }

}
